package com.cat.controller;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

import static com.cat.util.ServletUtils.showError;

/**
 * @author :HYC  email: kobe524348@gmail.com
 * @program : HYC_CatRecruit
 * @name : LogOutServlet
 * @description : 处理用户退出登陆的请求，本servlet会把session设置为无效，并且跳转到首页
 * @create : 2019-03-26 20:42
 */
@WebServlet("/LogOutServlet")
public class LogOutServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        this.doPost(req, res);
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        String method = req.getParameter("method");
        System.out.println("com.cat.controler.LogoutServlet : request method is " + method);
        HttpSession sess = req.getSession();
        if ("logout".equalsIgnoreCase(method)) {
            if (req.getAttribute("login") != null || sess.getAttribute("login") != null) {
                //注销用户
                showError(req, res, "注销成功 ： 您已退出登陆", "提示信息 : 如需进行其他操作，请重新登陆");
                req.getSession().invalidate();
            } else {
                res.sendRedirect("index.jsp");
            }
        }
    }
}